/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: settingswidget.cpp 397 2009-05-09 10:22:16Z jpnurmi $
*/

#include "settingswidget.h"
#include "settingshandler.h"
#include "connectdialog.h"
#include <QApplication>
#include <QMessageBox>
#include <QSettings>
#include <QAction>
#include <QStyle>
#include <QMenu>

SettingsHandler* SettingsWidget::settingsHandler = 0;

SettingsWidget::SettingsWidget(QWidget* parent) :
    QToolButton(parent)
{
    if (!settingsHandler)
        settingsHandler = new SettingsHandler(window());

    setIcon(QApplication::windowIcon());
    setPopupMode(QToolButton::InstantPopup);
    setMenu(new QMenu(this));

    connectMenu = new QMenu(tr("Connections"), window());
    connect(connectMenu, SIGNAL(aboutToShow()), this, SLOT(fillConnectMenu()));
    menu()->addMenu(connectMenu);

    QMenu* messageMenu = menu()->addMenu(tr("Messages"));
    fillSettingsMenu(messageMenu, SettingsHandler::Message);

    QMenu* highlightMenu = menu()->addMenu(tr("Highlights"));
    fillSettingsMenu(highlightMenu, SettingsHandler::Highlight);

    // TODO: should be no more called "SettingsWidget" 
    // as it contains more than just settings
    menu()->addSeparator();
    menu()->addAction(tr("About Commini"), this, SLOT(aboutCommini()));
    menu()->addAction(tr("About Qt"), qApp, SLOT(aboutQt()));
}

bool SettingsWidget::showJoins() const
{
    return settingsHandler->testSetting(SettingsHandler::Message, SettingsHandler::Joins);
}

bool SettingsWidget::showParts() const
{
    return settingsHandler->testSetting(SettingsHandler::Message, SettingsHandler::Parts);
}

bool SettingsWidget::showNicks() const
{
    return settingsHandler->testSetting(SettingsHandler::Message, SettingsHandler::Nicks);
}

bool SettingsWidget::showModes() const
{
    return settingsHandler->testSetting(SettingsHandler::Message, SettingsHandler::Modes);
}

bool SettingsWidget::showKicks() const
{
    return settingsHandler->testSetting(SettingsHandler::Message, SettingsHandler::Kicks);
}

bool SettingsWidget::showQuits() const
{
    return settingsHandler->testSetting(SettingsHandler::Message, SettingsHandler::Quits);
}

bool SettingsWidget::showTopics() const
{
    return settingsHandler->testSetting(SettingsHandler::Message, SettingsHandler::Topics);
}

bool SettingsWidget::highlightJoins() const
{
    return settingsHandler->testSetting(SettingsHandler::Highlight, SettingsHandler::Joins);
}

bool SettingsWidget::highlightParts() const
{
    return settingsHandler->testSetting(SettingsHandler::Highlight, SettingsHandler::Parts);
}

bool SettingsWidget::highlightNicks() const
{
    return settingsHandler->testSetting(SettingsHandler::Highlight, SettingsHandler::Nicks);
}

bool SettingsWidget::highlightModes() const
{
    return settingsHandler->testSetting(SettingsHandler::Highlight, SettingsHandler::Modes);
}

bool SettingsWidget::highlightKicks() const
{
    return settingsHandler->testSetting(SettingsHandler::Highlight, SettingsHandler::Kicks);
}

bool SettingsWidget::highlightQuits() const
{
    return settingsHandler->testSetting(SettingsHandler::Highlight, SettingsHandler::Quits);
}

bool SettingsWidget::highlightTopics() const
{
    return settingsHandler->testSetting(SettingsHandler::Highlight, SettingsHandler::Topics);
}

void SettingsWidget::fillSettingsMenu(QMenu* menu, uint itype)
{
    SettingsHandler::Type type = static_cast<SettingsHandler::Type>(itype);
    menu->addAction(settingsHandler->action(type, SettingsHandler::Joins));
    menu->addAction(settingsHandler->action(type, SettingsHandler::Parts));
    menu->addAction(settingsHandler->action(type, SettingsHandler::Nicks));
    menu->addAction(settingsHandler->action(type, SettingsHandler::Modes));
    menu->addAction(settingsHandler->action(type, SettingsHandler::Kicks));
    menu->addAction(settingsHandler->action(type, SettingsHandler::Topics));
    menu->addAction(settingsHandler->action(type, SettingsHandler::Quits));
    menu->addSeparator();
    menu->addAction(settingsHandler->action(type, SettingsHandler::All));
}

void SettingsWidget::fillConnectMenu()
{
    connectMenu->clear();
    foreach (const Connection& connection, Connection::connections())
    {
        QMenu* menu = new QMenu(connectMenu);
        menu->setTitle(connection.name);
        connectMenu->addMenu(menu);

        QAction* connectAction = menu->addAction(tr("Connect"), this, SLOT(startConnection()));
        connectAction->setData(connection);
        QAction* editAction = menu->addAction(tr("Edit"), this, SLOT(editConnection()));
        editAction->setData(connection);
        QAction* removeAction = menu->addAction(tr("Remove"), this, SLOT(removeConnection()));
        removeAction->setData(connection);
    }
}

void SettingsWidget::startConnection()
{
    QAction* action = qobject_cast<QAction*>(sender());
    if (action)
        emit connectTo(action->data().value<Connection>());
}

void SettingsWidget::editConnection()
{
    QAction* action = qobject_cast<QAction*>(sender());
    if (action)
    {
        Connection backup = action->data().value<Connection>();

        ConnectDialog dialog;
        dialog.setModifying(true);
        dialog.setConnection(backup);
        if (dialog.exec())
        {
            Connection conn = dialog.connection();
            int index = Connection::connections().indexOf(backup);
            Connection::save(conn, index);
        }
    }
}

void SettingsWidget::removeConnection()
{
    QAction* action = qobject_cast<QAction*>(sender());
    if (action)
    {
        // TODO: confirmation message
        Connection conn = action->data().value<Connection>();
        Connection::remove(conn);
    }
}

void SettingsWidget::aboutCommini()
{
    QMessageBox::about(this, QApplication::applicationName(), tr(
        "<h3>About Commini</h3>"
        "<p>Copyright (C) 2008-2009 J-P Nurmi <a href='mailto:jpnurmi@gmail.com'>jpnurmi@gmail.com</a></p>"
        "<p><a href='http://communi.googlecode.com'>http://communi.googlecode.com</a></p>"
        ));
}
